<?php
ini_set('default_charset','UTF-8');
setlocale(LC_ALL,'ru_RU.UTF-8');

require_once(_root.'/sys/dbsimple/Generic.php');

/*
$sqltbl_bugs     = "bugs";
$sqltbl_comments = "comments";
$sqltbl_users    = "users";
$sqltbl_places   = "places";
$sqltbl_types    = "types";
$sqltbl_modules  = "modules";
$sqltbl_rights   = "rights";
$sqltbl_urights  = "user_rights";
*/

$_mysql_error = false;

function mysql_log($msg)
{
   if(! _mysql_log_enable) return false;
   
   $msg = $msg."\n--------------\n\n";

   $f = fopen(_mysql_log, 'a');
   flock ($f, LOCK_EX);
   fwrite($f, $msg);
   flock ($f, LOCK_UN);
   fclose($f);
}


function mysql_ErrorHandler($message, $info)
{
   $ex = 'SQL Error: '.$message."\n ".print_r($info, true);
   mysql_log($ex);
}


function mysql_init()
{
   $host = _mysqlhost;
   $user = _mysqluser;
   $pass = _mysqlpass;
   $dbse = _mysqldbse;

   $db = DbSimple_Generic::connect('mysql://'.$user.':'.$pass.'@'.$host.'/'.$dbse);

   $db->setErrorHandler('mysql_ErrorHandler');
   
   if(!$db->link) die('<h1>MYSQL Error! Look at mysql.log.</h1>');

   $db->query("set names 'utf8'");
   $db->query("set character_set_results='utf8'");

   if($db) return $db;
   else return false;
}
?>
